package com.demoandroid.demo.x5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.demoandroid.demo.R;
import com.tencent.smtt.sdk.TbsReaderView;
import java.io.File;

/*
 * @DateTime 2020/6/29 16:51
 * @Author zwb
 * @Des X5内核文件预览页面
 * */
public class X5TbsFileServicePage extends AppCompatActivity implements TbsReaderView.ReaderCallback {

    private String tbsReaderTemp = Environment.getExternalStorageDirectory() + "/TbsReaderTemp";
    TbsReaderView mTbsReaderView;
    RelativeLayout mRelativeLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_x5_tbs_file_service_page);
        mRelativeLayout = findViewById(R.id.X5TbsView);
        FileModel fileModel = new FileModel();
        fileModel.setPath("/storage/emulated/0/demo/word.docx");
        displayFile(fileModel);
    }

    public void displayFile(FileModel fileModel) {
        mTbsReaderView = new TbsReaderView(this,this);
        //增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败
        String bsReaderTemp = tbsReaderTemp;
        File bsReaderTempFile =new File(bsReaderTemp);

        if (!bsReaderTempFile.exists()) {
            Log.d("print","文件不存在准备创建/TbsReaderTemp！！");
            boolean mkdir = bsReaderTempFile.mkdir();
            if(!mkdir){
                Log.d("print","创建/TbsReaderTemp失败！！！！！");
            }
            Toast.makeText(this,"文件不存在！",Toast.LENGTH_SHORT).show();
        }
        mRelativeLayout.addView(mTbsReaderView,new RelativeLayout.LayoutParams(-1,-1));
        Bundle bundle = new Bundle();
        bundle.putString("filePath",fileModel.getPath());
        bundle.putString("tempPath", tbsReaderTemp);
        boolean result = mTbsReaderView.preOpen(getFileType("word.docx"), false);
        Log.d("print","查看文档---"+result);
        if (result) {
            mTbsReaderView.openFile(bundle);
        }else{

        }
    }

    private String getFileType(String paramString) {
        String str = "";

        if (TextUtils.isEmpty(paramString)) {
            Log.d("print", "paramString---->null");
            return str;
        }
        Log.d("print", "paramString:" + paramString);
        int i = paramString.lastIndexOf('.');
        if (i <= -1) {
            Log.d("print", "i <= -1");
            return str;
        }

        str = paramString.substring(i + 1);
        Log.d("print", "paramString.substring(i + 1)------>" + str);
        return str;
    }

    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {
        Log.i("触摸监听："," ");
    }

    /**
     * 返回上个页面
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTbsReaderView != null) {
            mTbsReaderView.onStop();
            mTbsReaderView.destroyDrawingCache();
        }
    }

}